from inspect import cleandoc
import os 
if os.name == 'nt':
    os.chdir("C:\\federated_malware\\Project")
    print ("cwd",os.getcwd())#获得当前目录
    print ("工作目录",os.path.abspath('.'))#获得当前工作目录
    print ("工作目录",os.path.abspath(os.curdir))#获得当前工作目录
'''测试说明文档
完成进度
    数据集加载：
        generated,
        Minist,
        Feminist,
        maldroid,
    数据划分算法：
        avg,
        iid,
        noniid,
        noniid_by_label_number,
        noniid_by_label_distribution,
        noniid_by_feature_bias,
        noniid_by_real
        noniid_by_quantity,
    非独立同分布衡量方法：TL散度（需要继续调研）
'''

# 所有数据集数据划分的测试用例计算的测试用例------------------------------------------------------------------------------
from datasets.minist.data_load import dataManager as mdm
# from datasets.feminist.data_load import dataManager as fdm 
# from datasets.maldroid.data_load import dataManager as maldm 


# 只需要能跑过就行，统计的返回值Counter符合要求即可
d = mdm.allocate_data_avg()
d = mdm.allocate_data_iid()
d = mdm.allocate_data_noniid()
d = mdm.allocate_data_noniid_by_label_number()
d = mdm.allocate_data_noniid_by_label_number2()
d = mdm.allocate_data_noniid_by_label_distribution()
d = mdm.allocate_data_noniid_by_feature_noise()
d = mdm.allocate_data_noniid_by_quantity()
d = mdm.allocate_data_test()

d = fdm.allocate_data_noniid()
d = fdm.allocate_data_test()


d = maldm.allocate_data_avg(client_number=20)
d = maldm.allocate_data_iid(client_number=20,data_counts=800)
d = maldm.allocate_data_noniid(client_number=20,data_counts=800,class_total=5,class_counts=2)
d = maldm.allocate_data_noniid_by_label_number(client_number=20,data_counts=800,class_total=5,class_counts=2)
d = maldm.allocate_data_noniid_by_label_distribution(client_number=20,class_total=5,beta=0.4)
d = maldm.allocate_data_noniid_by_feature_noise(client_number=20,bias_level=0.1)
d = maldm.allocate_data_noniid_by_quantity(client_number=20,class_total=5,beta=0.4)
d = maldm.allocate_data_test()

# wasserstein距离计算的测试用例------------------------------------------------------------------------------
from datasets.minist.data_load import dataManager as mdm
from datasets.feminist.data_load import dataManager as fdm 
from datasets.maldroid.data_load import dataManager as maldm 

# 用来测试数据集离散程度函数mdm
dataset_avg = mdm.allocate_data_avg()
dx_avg = mdm.calculate_degree_x_by_Wasserstein(dataset_avg)
dy_avg = mdm.calculate_degree_y_by_Wasserstein(dataset_avg)

dataset_iid = mdm.allocate_data_iid()
dx_iid = mdm.calculate_degree_x_by_Wasserstein(dataset_iid)
dy_iid = mdm.calculate_degree_y_by_Wasserstein(dataset_iid)
assert abs(dx_iid-dx_avg)<1
assert abs(dy_iid-dy_avg)<1


dataset_noniid = mdm.allocate_data_noniid()
dy_noniid = mdm.calculate_degree_y_by_Wasserstein(dataset_noniid)
assert abs(dy_noniid-dy_avg)>1

dataset_ln = mdm.allocate_data_noniid_by_label_number()
dy_ln = mdm.calculate_degree_y_by_Wasserstein(dataset_ln)
assert abs(dy_ln-dy_avg)>1


dataset_ld = mdm.allocate_data_noniid_by_label_distribution()
dy_ld = mdm.calculate_degree_y_by_Wasserstein(dataset_ld)
assert abs(dy_ld-dy_avg)>1


dataset_fn = mdm.allocate_data_noniid_by_feature_noise()
dx_fn = mdm.calculate_degree_x_by_Wasserstein(dataset_fn)
assert abs(dx_fn-dx_avg)>1


datset_quantity = mdm.allocate_data_noniid_by_quantity()
dx_quantity = mdm.calculate_degree_x_by_Wasserstein(datset_quantity)
dy_quantity = mdm.calculate_degree_y_by_Wasserstein(datset_quantity)
assert abs(dx_quantity-dx_avg)<1
assert abs(dy_quantity-dy_avg)<1





# 用来测试数据集离散程度函数fdm的离散程度。只有一种真实数据的分割方法


dataset_noniid = fdm.allocate_data_noniid()
dx_noniid = fdm.calculate_degree_x_by_Wasserstein(dataset_noniid)
dy_noniid = fdm.calculate_degree_y_by_Wasserstein(dataset_noniid)


# 用来测试数据集离散程度函数maldm.多种分割方法尝试
dataset_avg = maldm.allocate_data_avg(client_number=20)
dx_avg = maldm.calculate_degree_x_by_Wasserstein(dataset_avg)
dy_avg = maldm.calculate_degree_y_by_Wasserstein(dataset_avg)

dataset_iid = maldm.allocate_data_iid(client_number=20,data_counts=800)
dx_iid = maldm.calculate_degree_x_by_Wasserstein(dataset_iid)
dy_iid = maldm.calculate_degree_y_by_Wasserstein(dataset_iid)
assert abs(dx_iid-dx_avg)<1
assert abs(dy_iid-dy_avg)<1


dataset_noniid = maldm.allocate_data_noniid(client_number=20,data_counts=800,class_total=5,class_counts=2)
dy_noniid = maldm.calculate_degree_y_by_Wasserstein(dataset_noniid)
assert abs(dy_noniid-dy_avg)>0.5

dataset_ln = maldm.allocate_data_noniid_by_label_number(client_number=20,data_counts=800,class_total=5,class_counts=2)
dy_ln = maldm.calculate_degree_y_by_Wasserstein(dataset_ln)
assert abs(dy_ln-dy_avg)>1


dataset_ld = maldm.allocate_data_noniid_by_label_distribution(client_number=20,class_total=5,beta=0.4)
dy_ld = maldm.calculate_degree_y_by_Wasserstein(dataset_ld)
assert abs(dy_ld-dy_avg)>1


dataset_fn = maldm.allocate_data_noniid_by_feature_noise(client_number=20,bias_level=0.1)
dx_fn = maldm.calculate_degree_x_by_Wasserstein(dataset_fn)
# 因为已经归一化了数据，所以元数据方差普遍为1.所以0.1方差添加，最多引起0.1的变动
# assert abs(dx_fn-dx_avg)>0.1


datset_quantity = maldm.allocate_data_noniid_by_quantity(client_number=20,class_total=5,beta=0.4)
dx_quantity = maldm.calculate_degree_x_by_Wasserstein(datset_quantity)
dy_quantity = maldm.calculate_degree_y_by_Wasserstein(datset_quantity)
assert abs(dx_quantity-dx_avg)<1
assert abs(dy_quantity-dy_avg)<1


